﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Web.Security;

public partial class Users_RescheduleTicket1 : System.Web.UI.Page
{

    int oldprice, newprice;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack && Session["OldTicket"] == null && Session["NewTicket"] == null)
        {
            Response.Redirect("~/Users/TicketList.aspx");
        }

        if (!Page.IsPostBack)
        {
            Ticket newTicket = (Ticket)Session["NewTicket"];
            Ticket oldTicket = (Ticket)Session["OldTicket"];

            TicketBAL _ticketBAL = new TicketBAL();

            oldprice = 0;
            foreach (TicketDetail detail in oldTicket.TicketDetail)
            {
                oldprice += detail.Price;
            }
            lblPrice.Text = oldprice.ToString() + " USD";

            int refund = _ticketBAL.getDiscount(oldTicket.Flight.DepartTime, "Cancel");
            lblRefundpc.Text = refund.ToString() + " %";

            int totalOld = Convert.ToInt32(oldprice * refund / 100);
            lblRefundUsd.Text = totalOld.ToString() + " USD";

            string name = "From : " + newTicket.Flight.Route.Origin.FullName + "<br />To : " + newTicket.Flight.Route.Origin.FullName + "<br/>Depart Time : " + newTicket.Flight.DepartTime.ToString();

            List<Ticket> temp = new List<Ticket>();
            temp.Add(newTicket);

            BuyTable table = new BuyTable(ref temp, name, newTicket.TicketDetail[0].SeatClass, _ticketBAL.getDiscount(newTicket.Flight.DepartTime, "Buy"));

            PlaceHolder1.Controls.Add(table);
            newprice = table.TotalPrice;

            int total = newprice - totalOld;
            if (total >= 0)
            {
                lblConfirm.Text = "To reschedule, you will have to pay : " + total.ToString() + " USD.";
            }
            else
            {
                lblConfirm.Text = "after reschedule, we will give back : " + ((-1) * total).ToString() + " USD.";
            } 
        }
    }

    private class BuyTable : Table
    {
        private int _totalPrice;
        public int TotalPrice
        {
            get { return _totalPrice; }
            set { _totalPrice = value; }
        }

        public BuyTable(ref List<Ticket> _ticketList, string name, string _seatClass, int _discount)
        {
            int baseprice = 0;
            #region get base price
            foreach (Ticket ticket in _ticketList)
            {
                if (_seatClass == "Bussiness")
                {
                    baseprice += ticket.Flight.BussPrice;
                }
                else if (_seatClass == "First")
                {
                    baseprice += ticket.Flight.FirstPrice;
                }
                else if (_seatClass == "Club")
                {
                    baseprice += ticket.Flight.ClubPrice;
                }
            }
            #endregion

            int numofAdult = 0;
            int priceofAdult = 0;
            int numofChild = 0;
            int priceofChild = 0;
            int numofSenior = 0;
            int priceofSenior = 0;
            foreach (Ticket ticket in _ticketList)
            {
                foreach (TicketDetail detail in ticket.TicketDetail)
                {
                    if (detail.Passenger.Age < 12)
                    #region child price
                    {
                        if (_seatClass == "Bussiness")
                        {
                            detail.Price = Convert.ToInt32(ticket.Flight.BussPrice - (ticket.Flight.BussPrice * (ticket.Flight.ChildDiscount + _discount) / 100));
                        }
                        else if (_seatClass == "First")
                        {
                            detail.Price = Convert.ToInt32(ticket.Flight.FirstPrice - (ticket.Flight.FirstPrice * (ticket.Flight.ChildDiscount + _discount) / 100));
                        }
                        else if (_seatClass == "Club")
                        {
                            detail.Price = Convert.ToInt32(ticket.Flight.ClubPrice - (ticket.Flight.ClubPrice * (ticket.Flight.ChildDiscount + _discount) / 100));
                        }
                        numofChild++;
                        priceofChild += detail.Price;
                    }
                    #endregion
                    else if (detail.Passenger.Age >= 65)
                    #region senior price
                    {
                        if (_seatClass == "Bussiness")
                        {
                            detail.Price = Convert.ToInt32(ticket.Flight.BussPrice - (ticket.Flight.BussPrice * (ticket.Flight.SeniorDiscount + _discount) / 100));
                        }
                        else if (_seatClass == "First")
                        {
                            detail.Price = Convert.ToInt32(ticket.Flight.FirstPrice - (ticket.Flight.FirstPrice * (ticket.Flight.SeniorDiscount + _discount) / 100));
                        }
                        else if (_seatClass == "Club")
                        {
                            detail.Price = Convert.ToInt32(ticket.Flight.ClubPrice - (ticket.Flight.ClubPrice * (ticket.Flight.SeniorDiscount + _discount) / 100));
                        }
                        numofSenior++;
                        priceofSenior += detail.Price;
                    }
                    #endregion
                    else
                    #region adult price
                    {
                        if (_seatClass == "Bussiness")
                        {
                            detail.Price = Convert.ToInt32(ticket.Flight.BussPrice - (ticket.Flight.ClubPrice * _discount / 100));
                        }
                        else if (_seatClass == "First")
                        {
                            detail.Price = Convert.ToInt32(ticket.Flight.FirstPrice - (ticket.Flight.ClubPrice * _discount / 100));
                        }
                        else if (_seatClass == "Club")
                        {
                            detail.Price = Convert.ToInt32(ticket.Flight.ClubPrice - (ticket.Flight.ClubPrice * _discount / 100));
                        }
                        numofAdult++;
                        priceofAdult += detail.Price;
                    }
                    #endregion
                }
            }

            _totalPrice = numofAdult * priceofAdult + numofChild * priceofChild + numofSenior * priceofSenior;

            #region Row 1 header
            TableRow row1 = new TableRow();
            row1.Font.Bold = true;
            row1.BackColor = Color.AntiqueWhite;
            TableCell c1 = new TableCell();
            c1.ColumnSpan = 2;
            c1.Controls.Add(new LiteralControl(name));
            row1.Cells.Add(c1);
            #endregion
            this.Rows.Add(row1);

            #region Row 1.5 seat class
            TableRow row15 = new TableRow();
            TableCell c01 = new TableCell();
            c01.Controls.Add(new LiteralControl("Seat Class"));
            row15.Cells.Add(c01);
            TableCell c02 = new TableCell();
            c02.Controls.Add(new LiteralControl(_seatClass + " Class "));
            row15.Cells.Add(c02);
            #endregion
            this.Rows.Add(row15);

            #region Row 2 base price
            TableRow row2 = new TableRow();
            row2.BackColor = Color.Gray;
            row2.ForeColor = Color.White;

            TableCell c21 = new TableCell();
            c21.Controls.Add(new LiteralControl("Base Price"));
            row2.Controls.Add(c21);

            TableCell c22 = new TableCell();
            c22.Controls.Add(new LiteralControl(baseprice.ToString() + " USD"));
            row2.Cells.Add(c22);
            #endregion
            this.Rows.Add(row2);

            #region Row 2.5 Discount
            TableRow row25 = new TableRow();
            TableCell c251 = new TableCell();
            c251.Controls.Add(new LiteralControl("Discount"));
            row25.Cells.Add(c251);
            TableCell c252 = new TableCell();
            c252.Controls.Add(new LiteralControl(_discount + " % "));
            row25.Cells.Add(c252);
            #endregion
            this.Rows.Add(row25);

            #region Row 3 Adult Price
            TableRow row3 = new TableRow();

            TableCell c31 = new TableCell();
            c31.Controls.Add(new LiteralControl("Adult Price x " + numofAdult.ToString()));
            row3.Controls.Add(c31);

            TableCell c32 = new TableCell();
            c32.Controls.Add(new LiteralControl((priceofAdult * numofAdult).ToString() + " USD"));
            row3.Cells.Add(c32);
            #endregion
            this.Rows.Add(row3);

            #region Row 4 Child Price
            TableRow row4 = new TableRow();

            TableCell c41 = new TableCell();
            c41.Controls.Add(new LiteralControl("Child Price x " + numofChild.ToString() + " (with child discount)"));
            row4.Controls.Add(c41);

            TableCell c42 = new TableCell();
            c42.Controls.Add(new LiteralControl((priceofChild * numofChild).ToString() + " USD"));
            row4.Cells.Add(c42);
            #endregion
            this.Rows.Add(row4);

            #region Row 5 Senior Price
            TableRow row5 = new TableRow();

            TableCell c51 = new TableCell();
            c51.Controls.Add(new LiteralControl("Senior Price x " + numofSenior.ToString() + " (with senior discount)"));
            row5.Controls.Add(c51);

            TableCell c52 = new TableCell();
            c52.Controls.Add(new LiteralControl((priceofSenior * numofSenior).ToString() + " USD"));
            row5.Cells.Add(c52);
            #endregion
            this.Rows.Add(row5);

            #region Row 6 Total Price
            TableRow row6 = new TableRow();
            row6.BackColor = Color.LightBlue;
            row6.Font.Bold = true;

            TableCell c61 = new TableCell();
            c61.Controls.Add(new LiteralControl("Total Price"));
            row6.Controls.Add(c61);

            TableCell c62 = new TableCell();
            c62.Controls.Add(new LiteralControl(_totalPrice.ToString() + " USD"));
            row6.Cells.Add(c62);
            #endregion
            this.Rows.Add(row6);

            this.Width = Unit.Percentage(100);
            this.Font.Size = FontUnit.Large;
            this.BackColor = Color.White;
        }
    }


    protected void Button1_Click(object sender, EventArgs e)
    {
        TicketBAL _ticketBAL = new TicketBAL();
        Ticket oldTicket = (Ticket)Session["OldTicket"];
        Ticket newTicket = (Ticket)Session["NewTicket"];

        string userid = Membership.GetUser().ProviderUserKey.ToString();
        _ticketBAL.RescheduleTicket(oldTicket, newTicket, userid);

        lblConfirm.Text = "Reschedule Completed.";
        Button1.Enabled = false;
    }
}
